package cn.hx.mall.sell.controller;

import cn.hx.mall.sell.entity.SmsHomeBrand;
import cn.hx.mall.sell.entity.SmsHomeRecommendProduct;
import cn.hx.mall.sell.service.SmsHomeBrandService;
import cn.hx.mall.sell.service.SmsHomeRecommendProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 首页推荐品牌表(SmsHomeBrand)表控制层
 *
 * @author makejava
 * @since 2022-11-02 16:10:31
 */
@Controller
@RequestMapping("sell")
public class SmsHomeRecommendProductController {
    /**
     * 服务对象
     */
    @Resource
    private SmsHomeRecommendProductService smsHomeRecommendProductService;

    @GetMapping("/product")
    public String brandList(ModelMap map, SmsHomeRecommendProduct smsHomeRecommendProduct,
                            @RequestParam(value = "page",defaultValue = "1") Integer page,
                            @RequestParam(value = "size", defaultValue = "5") Integer size) {
        Page<SmsHomeBrand> smsHomeBrands = PageHelper.startPage(page, size);
        smsHomeRecommendProductService.queryAll(smsHomeRecommendProduct);
        PageInfo<SmsHomeBrand> pageInfo = new PageInfo<>(smsHomeBrands);
        map.addAttribute("products", pageInfo);
        return "sell/product";
    }

    @GetMapping("deleteByProductId/{id}")
    @ResponseBody
    public Map<String, Object> delete(@PathVariable Long id) {
        boolean b = smsHomeRecommendProductService.deleteById(id);
        HashMap<String, Object> map = new HashMap<>();
        if(b) {
            map.put("code", 1);
        }
        return map;
    }

    @PostMapping("selectProduct")
    public String likeAll( SmsHomeRecommendProduct smsHomeRecommendProduct, ModelMap map, @RequestParam(value = "page",defaultValue = "1") Integer page,
                          @RequestParam(value = "size", defaultValue = "5") Integer size) {
        Page<SmsHomeRecommendProduct> smsHomeBrands = PageHelper.startPage(page, size);
        smsHomeRecommendProductService.queryAll(smsHomeRecommendProduct);
        PageInfo<SmsHomeRecommendProduct> pageInfo = new PageInfo<>(smsHomeBrands);
        map.addAttribute("products", pageInfo);
        return "sell/product";
    }

    @PostMapping("/updateProductSort")
    public String save(SmsHomeRecommendProduct smsHomeRecommendProduct,String path) {
        smsHomeRecommendProductService.update(smsHomeRecommendProduct);
        return "redirect:/"+path;
    }

    @GetMapping("/selectByProductId/{id}")
    @ResponseBody
    public Map<String, Object> queryById(@PathVariable Long id) {
        SmsHomeRecommendProduct smsHomeRecommendProduct = smsHomeRecommendProductService.queryById(id);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("data", smsHomeRecommendProduct);
        return resMap;
    }
    @GetMapping("/addProduct")
    public String add(){
        return "sell/addProduct";
    }
    @PostMapping("/addProduct")
    public String brand(ModelMap map,  SmsHomeRecommendProduct smsHomeRecommendProduct){
        smsHomeRecommendProductService.insert(smsHomeRecommendProduct);
        map.addAttribute("products",smsHomeRecommendProductService.queryAllByLimit(0,1000));
        return "redirect:/sell/product";
    }
    /*状态按钮改变*/
    @GetMapping("statusByProductId/{id}")
    @ResponseBody
    public Map<String, Object> statusById(@PathVariable Long id) {
        SmsHomeRecommendProduct smsHomeRecommendProduct = smsHomeRecommendProductService.queryById(id);
        Integer status = smsHomeRecommendProduct.getRecommendStatus();
        if (status.equals(1)) {
            smsHomeRecommendProduct.setRecommendStatus(0);
        } else {
            smsHomeRecommendProduct.setRecommendStatus(1);
        }
        HashMap<String,Object> map = new HashMap<>();
        SmsHomeRecommendProduct update = smsHomeRecommendProductService.update(smsHomeRecommendProduct);
        if(update!=null) {
            map.put("code", 1);
        }
        return map;
    }
}